package annotation.define.database;

import annotation.define.database.Constraints;
import annotation.define.database.MyTable;
import annotation.define.database.SqlInteger;
import annotation.define.database.SqlString;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * MyTable注解的处理器
 *
 * @author sun.ye
 * @since 2020-05-11 17:17
 */
public class TableCreator {

    public static void main(String[] args) throws ClassNotFoundException {
        if(args.length < 0){
            System.out.println("arguments : annotated classes");
            System.exit(0);
        }
        for (String className : args) {
            Class<?> cl = Class.forName(className);
            MyTable myTable = cl.getAnnotation(MyTable.class);
            if (myTable == null) {
                System.out.println("No MyTable annotation in class " + className);
                continue;
            }
            String tableName = myTable.name();
            if (tableName.length() < 1) {
                tableName = cl.getName().toUpperCase();
            }

            List<String> columDefs = new ArrayList<>();
            for (Field field : cl.getDeclaredFields()) {
                String columName = null;
                Annotation[] annotations = field.getDeclaredAnnotations();
                if (annotations.length < 1) {
                    //not a db table colum
                    continue;
                }
                if (annotations[0] instanceof SqlInteger) {
                    SqlInteger sqlInteger = (SqlInteger) annotations[0];
                    if (sqlInteger.name().length() < 1){
                        columName = field.getName().toUpperCase();
                    }else {
                        columName = sqlInteger.name();
                    }
                    columDefs.add(className + " INT " + getConstraints(sqlInteger.constraints()));
                }

                if (annotations[0] instanceof SqlString) {
                    SqlString sqlString = (SqlString) annotations[0];
                    if (sqlString.name().length() < 1){
                        columName = field.getName().toUpperCase();
                    }else {
                        columName = sqlString.name();
                    }
                    columDefs.add(className + " VARCHAR( " + getConstraints(sqlString.constraints()) + ")");
                }
                StringBuilder createCommand = new StringBuilder("CREATE TABLE " + tableName + "(");
                for (String columDef : columDefs) {
                    createCommand.append("\n    " + columDef + " , ");
                }
                String tableCreate = createCommand.substring(0,createCommand.length() - 1) + " ) ;";
                System.out.println("Table creation SQL for " + className + " is \n" + tableCreate);

            }
        }
    }


    private static String getConstraints(Constraints constraints){
        String constraintsStr = " ";
        if (constraints.allowNULL()) {
            constraintsStr += " NOT NULL ";
        }
        if (constraints.primaryKey()) {
            constraintsStr += " PRIMARY KEY ";
        }
        if (constraints.unique()){
            constraintsStr += " UNIQUE ";
        }
        return constraintsStr;

    }
}